<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1
 * @license    GNU/GPL
 * TODO
 * datetime not working
*/
 
// I must send post not share on this batchupload ... 
 
// no direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.filesystem.path' );
jimport( 'joomla.application.component.view');
jimport( 'joomla.database.database' );
include JPATH_COMPONENT.DS.'functions.php';




 
// Create a new query object.
//$query = $db->getQuery(true);
 
// Insert columns.
//$columns = array('user_id', 'profile_key', 'profile_value', 'ordering');
 
// Insert values.
//$values = array(1001, $db->quote('custom.message'), $db->quote('Inserting a record using insert()'), 1);
 
// Prepare the insert query.
//$query
//    ->insert($db->quoteName('#__user_profiles'))
//    ->columns($db->quoteName($columns))
//    ->values(implode(',', $values));
 
// Reset the query using our newly populated query object.
//$db->setQuery($query);


/**
 * HTML View class for the HelloWorld Component
 *
 * @package    HelloWorld
 */
class wotclanViewbatchupload extends JView
{
    function display($tpl = null)
    {
    	if (JFactory::getUser()->authorise('core.add', 'com_wotclan')) 
      {
		jimport('joomla.filesystem.file');
        $db =& JFactory::getDbo();
        $greeting = "Uploader votre replay! (V0.0.1)";
        $this->assignRef( 'greeting', $greeting );
        
        // get user info
        
    	$user =& JFactory::getUser();
		$usr_id = $user->get('id');
				
        $repuniq=uniqid("", true);
		$rep="media/wotclan/replays/".$repuniq;
		mkdir($rep);
         //Retrieve file details from uploaded file, sent from upload form
		$file = JRequest::getVar('file_upload', null, 'files', 'array');
 
				//Import filesystem libraries. Perhaps not necessary, but does not hurt
 
				//Clean up filename to get rid of strange characters like spaces etc
		$filename = JFile::makeSafe($file['name']);
        parent::display($tpl);
        if ($_FILES["file"]["error"] > 0)
  			{
  				echo "Error: " . $_FILES["file"]["error"] . "<br>";
  			}
				//else
  			//{
  			//	echo "Upload: " . $_FILES["file_upload"]["name"] . "<br>";
  			//	echo "Type: " . $_FILES["file_upload"]["type"] . "<br>";
  			//	echo "Size: " . ($_FILES["file_upload"]["size"] / 1024) . " kB<br>";
  			//	echo "Stored in: " ."media/wotclan/replays/" . $_FILES["file"]["name"]  ;
 
  			//}

 
				//Set up the source and destination of the file
				// $src = $file['tmp_name'];
				$src = $_FILES["file_upload"]['tmp_name'];
				$dest = JPATH_ROOT.DS. "media" . DS . "wotclan" . DS . "replays" . DS . $repuniq . DS . $_FILES["file_upload"]["name"];
 				//echo "<br>new dest : " . $dest ;

 
				//First check if the file has the right extension, we need jpg only
			if ( strtolower(JFile::getExt($filename) ) == 'zip') {
   			if ( JFile::upload($src, $dest) ) {
   		    	$zip = new ZipArchive;
                $res = $zip->open($dest);
                if ($res === TRUE) {
  					$zip->extractTo(JPATH_ROOT.DS. "media" . DS . "wotclan" . DS . "replays" . DS . $repuniq . DS);
  					for($i = 0; $i < $zip->numFiles; $i++) {
                    $filename = $zip->getNameIndex($i);
                    //echo "<h1>".$filename."</h1><br>";
                    $fp = fopen(JPATH_ROOT.DS. "media" . DS . "wotclan" . DS . "replays" . DS . $repuniq . DS . $filename , 'rb');
                    while ( ($line = fgets($fp)) !== false) {
    				    if ((preg_match("/mapName/i", $line)) || (preg_match("/frags/i", $line))){
    					    analyse_team($line,JPATH_ROOT.DS. "media" . DS . "wotclan" . DS . "replays" . DS . $repuniq . DS . $filename);
    				}

   				//echo "<br><br> line : ".$line."<br><br>";
      		}
      		
   						echo "<hr>";
            }
                    unlink($dest);
  					$zip->close();
  					//echo 'woot!';
          } else {
  					echo 'doh!';
          }
    
   	 			//Redirect to a page of your choice
    			echo "all is ok";
    
    				} else {
      		//Redirect and throw an error message
      		echo "error message";
   			}
		 } else {
   		//Redirect and notify user file is not right extension
   		echo "no right extension";
		 }
    } else {
    		$greeting = "Vous n'avez pas acces a cette section!";
        $this->assignRef( 'greeting', $greeting );
 
        parent::display($tpl);
    		
    }
	}
}




